package dao

import (
	"errors"
	"go-db/sqldb"
	"go-db/sqldb/entity"
)

type UserProfile struct {
	sqldb.DAO
}

func (m *UserProfile) NewEntity() interface{} {
	return &entity.UserProfile{}
}

func (m *UserProfile) Bind(db *sqldb.Database) {
	m.RegisterEntity(entity.UserProfile{})
	m.Name = "t_user_profile"
	m.DAO.Bind(db)
}

func (m *UserProfile) Get(qr *sqldb.QueryRules) ([]entity.UserProfile, error) {
	var list []entity.UserProfile
	err := m.DAO.Query(qr, m.NewEntity(), func(user interface{}) error {
		list = append(list, *(user.(*entity.UserProfile)))
		return nil
	})
	return list, err
}

func (m *UserProfile) GetById(id int64) (*entity.UserProfile, error) {
	c := &sqldb.Cond{}
	c.Reset().And("ID", "=", id)
	list, err := m.Get(&sqldb.QueryRules{C: c, S: nil, P: nil})
	if err != nil {
		return nil, err
	}
	if len(list) <= 0 {
		return nil, errors.New("[UserProfile::GetById] not found!")
	}
	return &list[0], nil
}
